// pages/addAddress/addAddress.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    customItem: '',
    region: ['请选择', '', ''],
    is_default:'',
    name: '',
    phone: '',
    addressdelt: '',
    address: '',
    id:'',
    address_name:'',
    do_type:'',
  },
  // 单个收货地址详情
  user_address_info(id){
    let that = this,
        is_default = this.data.is_default;
    wh.Post('/api/shop.User_address/user_address_info', {id:id}, function (res) {
      console.log('收货地址详情', res)
      if (res.is_default==1) {
        is_default =true
      } else {
        is_default =false
      }
      that.setData({
        address: res,
        region: res.area,
        addressdelt: res.address,
        phone: res.mobile,
        name: res.consignee,
        address_name: res.address_name,
        id: res.address_id,
        is_default: is_default
      })
    });
  },
  // 保存提交
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  switchChange: function (e) {
    this.setData({
      is_default: e.detail.value
    })
    console.log('switch1 发生 change 事件，携带值为', e.detail.value)
  },
  bindName: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  bindPhone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  bindAddress: function (e) {
    this.setData({
      addressdelt: e.detail.value
    })
  },
  submitContent: function () {
    let that=this,
     name = this.data.name,
     phone = this.data.phone,
     addressdelt = this.data.addressdelt,
     region = this.data.region,
     id = this.data.id,
      address_name = this.data.address_name,
     is_default = this.data.is_default,
      do_type = this.data.do_type;
    if (that.data.address){
      do_type = 'edit'
      }else{
      do_type = 'add'
      }
    if (!name) {
      wx.showToast({
        title: '请填写收件人信息',
        icon: "none"
      })
      return false;
    }
    if (!phone) {
      wx.showToast({
        title: '请填写联系电话',
        icon: "none"
      })
      return false;
    }
    if (!region[2]) {
      wx.showToast({
        title: '请选择所在地区',
        icon: "none"
      })
      return false;
    }
    if (!addressdelt) {
      wx.showToast({
        title: '请填写详细地址',
        icon: "none"
      })
      return false;
    }
    if (!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: '手机号格式错误',
        icon: "none"
      })
      return false;
    }
      wh.Post('/api/shop.User_address/save_user_address', {
        do_type: do_type,
        address_name: address_name,
        consignee:name,
        mobile:phone,
        address: addressdelt,
        province_name: region[0],
        city_name: region[1],
        district_name: region[2],
        id:id,
        is_default: is_default
      }, function(res){
        console.log('保存',res)
        wx.showToast({
          title: '保存成功',
          icon: 'none',
          duration: 2000,
          complete: function () {
            setTimeout(function () {
                let pages = getCurrentPages();
                let prevPage = pages[pages.length - 3];  //上一个页面
                wx.navigateBack({
                  delta: 1
                })     
            }, 2000);
          }
        });
      });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.id){
      this.user_address_info(options.id)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})